
const app = getApp();
let pageNo = 1;
let pageSize = 10;
Page({
  data: {
    vip:0,  //0非会员vip,1会员vip
    color:"#2F21BC",
    color2:"#ffffff",
    title:"塔罗入门：速通版",
    title_e:"Tarot for Beginners: Quick Pass",
    like:"108",
    liked: false,
    likeText:"加入喜欢",
    nums:"48",
    dataValues: ['20', '10'],
    urls:["/resource/images/p_1.png","/resource/images/p_2.png"],
    vipStatus:false,
    canvasWidth: 140, // 画布宽度
    progressWidths: [], // 进度条宽度数组
    showJianjie:true,
    lists: [
        {nums:"Part 1",title:"塔罗牌的历史和起源",showMl: false,
            infos:[{num2:"1.1",txt:"塔罗牌的起源与发展"},{num2:"1.2",txt:"主要塔罗牌流派"}]
        },
        {nums:"Part 2",title:"塔罗牌的基本结构",showMl: false,
            infos:[{num2:"2.1",txt:"大阿卡纳（Major Arcana）"},{num2:"2.2",txt:"小阿卡纳（Minor Arcana）"},{num2:"2.3",txt:"宫廷牌和数字牌"}]
        },
        {nums:"Part 3",title:"单张牌的基本解释",showMl: false,
            infos:[{num2:"3.1",txt:"22张大阿卡纳牌"},{num2:"3.2",txt:"小阿卡纳的四个花色"}]
        },
        {nums:"Part 4",title:"基础塔罗牌阵",showMl: false,
            infos:[{num2:"4.1",txt:"基础塔罗牌阵1"},{num2:"4.2",txt:"基础塔罗牌阵2"}]
        },
        {nums:"Part 5",title:"初步解读技巧",showMl: false,
            infos:[{num2:"5.1",txt:"初步解读技巧1"},{num2:"5.2",txt:"初步解读技巧2"}]
        },
    ],
    
    prize_class0:"8.8",
    specialPrize:"5.2",
    specialChecked: false,
    totalPrice: 0,
    quantity:0,
    vipList:[
      {id:1,name:"年卡会员",content:"购买后升级为",nums:"VIP 1",prize0:"598",prize:"299",checked:false},
    ],

    page: pageNo, //页数,
    loadmore: 'loadmorenone',//是否显示加载页,
    sessionid:'',
    navTop: app.globalData.navTop+6
  }, 
  attached: function () {
    // 定义导航栏的高度   方便对齐
    this.setData({
      navHeight: app.globalData.navHeight,
      navTop: app.globalData.navTop
    })
  },
    //   简介显隐
    Jianjie: function() {
        this.setData({
            showJianjie: !this.data.showJianjie
        });
    },
    //   目录显隐
    Ml: function(e) {
        const index = e.currentTarget.dataset.index;
        const lists = this.data.lists;
        lists[index].showMl = !lists[index].showMl;
        this.setData({
            lists: lists
        });
    },
    // 点赞
    liked: function() {
        let newLike = parseInt(this.data.like); // 将点赞数转换为整数
        newLike = this.data.liked ? newLike - 1 : newLike + 1;

        this.setData({
            like: newLike.toString(), // 将更新后的点赞数转换回字符串类型
            likeText: this.data.liked ? '加入喜欢' : '已加入喜欢',
            liked: !this.data.liked
        });
    },
    // 完结奖励数值
    drawProgress: function() {
        const dataValues = this.data.dataValues;
        const canvasWidth = this.data.canvasWidth;
        
        const progressWidths = dataValues.map(value => canvasWidth * value / 100);
        this.setData({
            progressWidths: progressWidths
        });
    },
    // 返回上一级
    back: function() {
        wx.navigateBack({
            delta: 1 // 返回上一级页面
        });
    },
  //优惠券显示对话框
  openVip: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  confirm: function (e) {
    let that = this;
    that.hideModal();
  },
  //选择  勾选
  vip_sel: function (e) {
        const index = parseInt(e.currentTarget.dataset.index, 10);

        if (index === 0) {
            // 如果点击的是第一个固定项目
            this.setData({
                specialChecked: !this.data.specialChecked
            });
        } else {
            // 如果点击的是vipList中的项目
            const checkedKey = `vipList[${index - 1}].checked`; // 数据索引比index少1
            this.setData({
                [checkedKey]: !this.data.vipList[index - 1].checked
            });
        }

        this.calculateTotalPrice();
    },

    calculateTotalPrice: function () {
        let totalPrice = 0;
        let quantity=0;

        // 计算第一个固定项目的价格
        if (this.data.specialChecked) {
            totalPrice += Number(this.data.specialPrize);
            quantity += 1;  // 件数加1
        }

        // 计算vipList中的价格
        this.data.vipList.forEach(item => {
            if (item.checked) {
                totalPrice += Number(item.prize);
                quantity += 1;  // 件数加1
            }
        });

        this.setData({
            totalPrice: totalPrice,
            quantity: quantity
        });
    },
  onLoad: function (options) {
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.drawProgress();  // 完结奖励数值
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})